{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-28T09:14:01.083367Z",
     "start_time": "2025-02-28T09:14:01.061499Z"
    }
   },
   "source": [
    "class Student:\n",
    "    name=None\n",
    "    age=None\n",
    "    def say_hi(self):\n",
    "        print(f\"我叫{self.name}\")\n",
    "    def say_hi2(self,msg):\n",
    "        print(f\"大家好，我是{self.name},{msg}\")\n",
    "        \n",
    "stu=Student()\n",
    "stu.name=\"周杰伦\"\n",
    "stu.say_hi()\n",
    "stu.say_hi2(\"哎呦不错呦\")"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我叫周杰伦\n",
      "大家好，我是周杰伦,哎呦不错呦\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#shift+insert光标（黑色方块）",
   "id": "ad4460365fa9048c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T09:23:51.582921Z",
     "start_time": "2025-02-28T09:23:48.482969Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#设置一个闹钟类\n",
    "class Clock:\n",
    "    id=None #序列号\n",
    "    price=None  #价格\n",
    "    \n",
    "    def ring(self):\n",
    "        import winsound\n",
    "        winsound.Beep(2000,3000)#频率，响铃时间\n",
    "        \n",
    "#构建2个闹钟对象并让其工作\n",
    "clock1=Clock()\n",
    "clock1.id=\"003032\"\n",
    "clock1.price=19.99\n",
    "print(f\"闹钟ID:{clock1.id},价格：{clock1.price}\")\n",
    "clock1.ring()\n",
    "        "
   ],
   "id": "ccf8ff096a020330",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "闹钟ID:003032,价格：19.99\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T09:32:51.016939Z",
     "start_time": "2025-02-28T09:32:51.007567Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#__init__\n",
    "class Student:\n",
    "    \"\"\"name=None\n",
    "    age=None\n",
    "    tel=None\"\"\"\n",
    "    def __init__(self,name,age,tel):\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "        self.tel=tel\n",
    "        print(\"Student类创建了一个类对象\")\n",
    "     \n",
    "stu=Student(\"周杰伦\",31,\"18500006666\")   \n",
    "print(stu.name,stu.age,stu.tel)"
   ],
   "id": "a5fb64d67f5f363d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Student类创建了一个类对象\n",
      "周杰伦 31 18500006666\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T09:55:10.531770Z",
     "start_time": "2025-02-28T09:55:10.522019Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#魔术方法\n",
    "class Student:\n",
    "    def __init__(self,name,age):\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "    #__str__魔术方法\n",
    "    def __str__(self):\n",
    "        return f\"Student类对象，name:{self.name},age:{self.age}\"\n",
    "    #__lt__\n",
    "    def __lt__(self,other):\n",
    "        return self.age<other.age\n",
    "\n",
    "stu1=Student(\"周杰伦\",31)\n",
    "stu2=Student(\"林俊杰\",13)\n",
    "print(stu)\n",
    "print(str(stu))\n",
    "print(stu1<stu2)\n",
    "print(stu1>stu2)"
   ],
   "id": "b5f95c01059b70dc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Student类对象，name:周杰伦,age:31\n",
      "Student类对象，name:周杰伦,age:31\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T10:21:19.247738Z",
     "start_time": "2025-02-28T10:21:19.229654Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#定义一个类，内含私有成员变量和私有成员方法\n",
    "class Phone:\n",
    "    __current_voltage=1 #当前手机运行电压\n",
    "    \n",
    "    def __keep_single_core(self):\n",
    "        print(\"让CPU以单核模式运行\")\n",
    "    \n",
    "    def call_by_5g(self):\n",
    "        if self.__current_voltage>=1:\n",
    "            print(\"5g通话已开启\")\n",
    "        else:\n",
    "            self.__keep_single_core()\n",
    "            print(\"电量不足，无法使用5g通话，并已设置为单核运行进行省电\")\n",
    "\n",
    "phone=Phone()\n",
    "phone.call_by_5g()"
   ],
   "id": "492cd47a514911dd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5g通话已开启\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T10:34:27.466812Z",
     "start_time": "2025-02-28T10:34:27.452469Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#继承\n",
    "class Phone:\n",
    "    IMEI=None\n",
    "    producer=\"HM\"\n",
    "    \n",
    "    def call_by_4g(self):\n",
    "        print(\"4g通话\")\n",
    "        \n",
    "class Phone2022(Phone):\n",
    "    face_id=\"10001\"\n",
    "    \n",
    "    def call_by_5g(self):\n",
    "        print(\"2022新功能：5g通话\")\n",
    "\n",
    "phone =Phone2022\n",
    "print(phone.producer)\n",
    "print(phone.IMEI)\n",
    "print(phone.face_id)"
   ],
   "id": "2b709a3c2973e5ab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HM\n",
      "None\n",
      "10001\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T11:00:54.259415Z",
     "start_time": "2025-02-28T11:00:54.243030Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class Phone:\n",
    "    IMEI=None\n",
    "    producer=\"HM\"\n",
    "    \n",
    "    def call_by_5g(self):\n",
    "        print(\"5g通话\")\n",
    "        \n",
    "class MyPhone(Phone):\n",
    "    producer=\"ITHEIMA\"\n",
    "    \n",
    "    def call_by_5g(self):\n",
    "        print(\"开启CPU单核模式，确保通话的时候省电\")\n",
    "        # #方式1\n",
    "        # print(f\"父类的厂商是：{Phone.producer}\")\n",
    "        # Phone.call_by_5g(self)\n",
    "        #方式2\n",
    "        print(f\"父类的厂商是：{super().producer}\")\n",
    "        super().call_by_5g()\n",
    "        print(\"关闭CPU单核模式，确保性能\")\n",
    "phone =MyPhone()\n",
    "phone.call_by_5g()\n",
    "print(phone.producer)"
   ],
   "id": "ed2820931f3a1883",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开启CPU单核模式，确保通话的时候省电\n",
      "父类的厂商是：HM\n",
      "5g通话\n",
      "关闭CPU单核模式，确保性能\n",
      "ITHEIMA\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T12:45:53.657577Z",
     "start_time": "2025-02-28T12:45:53.620776Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#多态\n",
    "class Animal:\n",
    "    def speak(self):\n",
    "        pass\n",
    "    \n",
    "class Dog(Animal):\n",
    "    def speak(self):\n",
    "        print(\"汪汪汪\")\n",
    "\n",
    "class Cat(Animal):\n",
    "    def speak(self):\n",
    "        print(\"喵喵喵\")\n",
    "        \n",
    "def make_noise(animal:Animal):\n",
    "    animal.speak()\n",
    "\n",
    "dog=Dog()\n",
    "cat=Cat()\n",
    "\n",
    "make_noise(dog)\n",
    "make_noise(cat)"
   ],
   "id": "8ba18c2f5838002c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "汪汪汪\n",
      "喵喵喵\n"
     ]
    }
   ],
   "execution_count": 17
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
